package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.po.*;
import com.example.demo.service.TBizOrderService;
import com.example.demo.service.WxUserAddressService;
import com.example.demo.util.CodeCache;
import com.example.demo.util.DateUtil;
import com.example.demo.util.WxUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/tBaOrder")
public class TBizOrderController {
    @Autowired
    TBizOrderService tBizOrderService;
    @Autowired
    WxUserAddressService wxUserAddressService;

    @RequestMapping(value = "/list")
    public ModelAndView list(@ModelAttribute TBizOrder orderInfo, HttpServletRequest request, HttpSession session){
        ModelAndView view = new ModelAndView();
        String type = orderInfo.getType();
        PageRequest page = new PageRequest();
        page.setPageNum(orderInfo.getPageNum());
        page.setPageSize(orderInfo.getPageSize());
        PageResult resilt = tBizOrderService.findByPage(orderInfo,page);
        //  字典值转换
        List<TBizOrder> list = (List<TBizOrder>) resilt.getContent();
        if(list!=null&&list.size()>0){
            Map<String, String> codeMap = CodeCache.codeMap;
            for (TBizOrder oa:list){
                oa.setOrderstateDesc(codeMap.get("orderstate_"+oa.getOrderstate()));
                System.out.println(oa.getSaleuserdesc());
            }
            resilt.setContent(list);
        }
        view.setViewName("/order/order_list");
        view.addObject("result", JSON.toJSON(resilt));
        String listName = null;
        if(type.equals("3"))
            listName="保证金订单";
        else  if(type.equals("2"))
            listName="拍卖会订单";
        else
            listName="商城订单";
        view.addObject("listName", listName);
        view.addObject("type", type);
        return  view;
    }


    @RequestMapping(value = "/updateInit")
    public ModelAndView updateInit(@ModelAttribute(name ="orderId") String orderId,HttpServletRequest request, HttpSession session){
        ModelAndView view = new ModelAndView();
        System.out.println(orderId);
        TBizOrder orderInfo =tBizOrderService.findById(orderId);
        view.addObject("order",orderInfo);
        view.setViewName("/order/order_edit");
        return  view;
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public String update(@RequestBody TBizOrder orderInfo, HttpServletRequest request, HttpSession session){
        JSONObject jsonObject = new JSONObject();
        try{
            if(tBizOrderService.Update(orderInfo)){
                //获取收货信息
                TBizOrder orderInfo1 = tBizOrderService.findById(orderInfo.getOrderid());
                String aid = orderInfo1.getAddress();
                WxUserAddress addressInfo =wxUserAddressService.findById(Integer.valueOf(aid));
                Map<String, String> map = new HashMap<>();
                map.put("key1",orderInfo.getCouriernumber());
                map.put("key2", orderInfo.getOrderid());
                map.put("key3",addressInfo.getUserName());
                map.put("key4",addressInfo.getAddress());
                WxUtil.sendType(orderInfo1.getUserid(),"4",map);
                jsonObject.put("oper_res",true);
            }else{
                jsonObject.put("oper_res",false);
            }
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("oper_res",false);
        }
        return  jsonObject.toJSONString();
    }

    @RequestMapping(value = "/backBZJ")
    public String backBZJ(@ModelAttribute(value = "orderId") String orderId,
                          @ModelAttribute(value = "ticketid") String ticketid,
                          @ModelAttribute(value = "price") String price,
                          HttpServletRequest request, HttpSession session){
        JSONObject jsonObject = new JSONObject();
        try{
            Double prices = Double.valueOf(price)*100;
            DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
            String p =decimalFormat.format(prices);

            if(WxUtil.returnMoney(ticketid,orderId,p)){
                TBizOrder orderInfo =new TBizOrder();
                orderInfo.setOrderid(orderId);
                orderInfo.setJzstate("4");
                orderInfo.setOrderstate("4");
                if(tBizOrderService.Update(orderInfo)){
                    jsonObject.put("oper_res",true);
                    jsonObject.put("data","退款成功");
                }else{
                    jsonObject.put("oper_res",false);
                    jsonObject.put("data","退款成功，修改状态失败");
                }

            }else{
                jsonObject.put("oper_res",false);
                jsonObject.put("data","退款失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("oper_res",false);
        }
        return  jsonObject.toJSONString();
    }

}
